<template>
    <div class="group-category">
        <div class="search-content">
            <el-select v-model="searchContent.status" placeholder="请选择状态">
                <el-option
                        v-for="item in statusList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                </el-option>
            </el-select>
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button type="primary" @click="showAddOrEdit">添加分类</el-button>
        </div>
        <div class="search-list">
            <el-table :data="groupCategoryList" border :height="oHeight">
                <el-table-column
                        align="center"
                        prop="id"
                        label="ID"
                        min-width="60"></el-table-column>
                <el-table-column
                        align="center"
                        prop="name"
                        label="分类名称"
                        min-width="120"></el-table-column>
                <el-table-column
                        align="center"
                        label="状态"
                        min-width="120">
                    <template slot-scope="scope">
                        <span>{{scope.row.status == 1 ? '启用' : '禁用'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        label="操作"
                        min-width="120">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="showAddOrEdit(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="bottom">
                <div class="btn-group">
                    <a class="btn-bottom" href="">导出</a>
                </div>
                <div class="paginationOne">
                    <el-pagination
                            @size-change="handleSizeChange"
                            @current-change="handleCurrentChange"
                            :current-page="searchContent.currentPage"
                            :page-sizes="[10, 20, 50, 100]"
                            :page-size="searchContent.pageSize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total="total">
                    </el-pagination>
                </div>
            </div>
        </div>
        <div class="addOrEdit-popup" :class="addOrEditCategoryShow ? 'show' : ''">
            <div class="addOrEdit-popup-bg"></div>
            <div class="addOrEdit-popup-content">
                <div class="addOrEdit-popup-content-heading">
                    <span class="addOrEdit-popup-content-heading-title">{{addOrEditCategory.id ? '编辑' : '添加'}}团购分类</span>
                    <span @click="closeAddOrEdit" class="el-icon-close"></span>
                </div>
                <div class="addOrEdit-popup-content-body">
                    <div class="input-group">
                        <span class="input-group-label">分类名称</span>
                        <el-input class="input-group-input" v-model="addOrEditCategory.name"></el-input>
                    </div>
                    <div class="input-group">
                        <span class="input-group-label">状态</span>
                        <el-select class="input-group-select" v-model="addOrEditCategory.status" placeholder="请选择状态">
                            <el-option
                                    v-for="item in statusList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value">
                            </el-option>
                        </el-select>
                    </div>
                </div>
                <div class="addOrEdit-popup-content-footer">
                    <el-button @click="comfirmAddOrEdit" style="margin-left: 15px" type="primary">确定</el-button>
                    <el-button @click="closeAddOrEdit">取消</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
	import { resize } from "@/common/js/height.js";

	export default {
		data() {
			return {
				addOrEditCategoryShow: false,
				addOrEditCategory: {
					id: '',
                    name: '',
                    status: 0
                },
				statusList: [
					{label: '启用', value: 1},
					{label: '禁用', value: 0}
				],
				oHeight: 800,
				searchContent: {
					status: ''
				},
				groupCategoryList: [],
				total: 0
			}
		},
		methods: {
			showAddOrEdit(item) {
				if (item) {
					this.addOrEditCategory.id = item.id;
					this.addOrEditCategory.name = item.name;
					this.addOrEditCategory.status = item.status;
                }
				this.addOrEditCategoryShow = true;
            },
			comfirmAddOrEdit() {
				const data = {};
				if (this.addOrEditCategory.id) {
					data.id = this.addOrEditCategory.id;
                }
				if (! this.addOrEditCategory.name) {
					alert('分类名称不能为空');
					return;
                } else {
					data.type_name = this.addOrEditCategory.name;
                }
				data.is_show = this.addOrEditCategory.status;
				this.$http.post('/admin/afterSalesGroupBuy/index.php?act=groupBuyClass&op=addOrEdit', data).then(res => {
					if (res.code == 1) {
						if (this.addOrEditCategory.id) {
							const groupCategory = this.groupCategoryList.find(item => item.id == this.addOrEditCategory.id);
							groupCategory.name = this.addOrEditCategory.name;
							groupCategory.status = this.addOrEditCategory.status;
                        } else {
							this.search();
                        }
						this.closeAddOrEdit();
                    } else {
						alert(res.msg);
                    }
                })
            },
			closeAddOrEdit() {
				this.addOrEditCategoryShow = false;
				this.addOrEditCategory.id = '';
				this.addOrEditCategory.name = '';
				this.addOrEditCategory.status = '';
            },
			resetSearchData() {
				this.searchContent.status = '';
			},
			search() {
				this.searchContent.currentPage = 1;
				this.getListData();
			},
			handleSizeChange(pageSize) {
				this.searchContent.pageSize = pageSize;
				this.searchContent.currentPage = 1;
				this.getListData();
			},
			handleCurrentChange(page) {
				this.searchContent.currentPage = page;
				this.getListData();
			},
			getListData() {
				const data = {};
				data.page = this.searchContent.currentPage;
				data.eachNum = this.searchContent.pageSize;
				data.is_show = this.searchContent.status;
				this.$http.post('/admin/afterSalesGroupBuy/index.php?act=groupBuyClass&op=list', data).then(res => {
					if (res.code == 1) {
						if (res.data && res.data.list.length) {
							this.groupCategoryList = res.data.list.map(function(item) {
								return {
									id: item.asc_id,
									name: item.asc_class_name,
									status: item.asc_is_show * 1
								}
							});
							this.total = parseInt(res.data.totalNum);
						} else {
							this.groupCategoryList = [];
							this.total = 0;
						}
					}
					else if (res.code == 2001) {
						this.groupCategoryList = [];
						this.total = 0;
					}
					else {
						alert(res.msg);
					}
				})
			}
		},
		mounted() {
			resize(this)
		},
		created() {
			if (this.$parent.tabLists[1] && this.$parent.tabLists[1].label == "审核" && this.$parent.tabLists[1].name == "group-detail") {
				this.$parent.tabLists.splice(1, 1);
			}
			this.getListData();
		},
		name: "group-category"
	}
</script>

<style lang="scss" scoped>
    .group-category {
        .search-content {
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            &-input {
                width: 200px;
            }
            .time-range-wrapper {
                .time-range-label {
                    font-size: 14px;
                }
                display: flex;
                align-items: center;
                /deep/.el-range-separator {
                    box-sizing: content-box;
                }
            }
        }
        .bottom {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
    }
    .btn-bottom {
        background: #409EFF;
        color: #fff;
        border: 0;
        font-size: 14px;
        padding: 5px 8px;
        border-radius: 3px;
        display: inline-block;
    }
    .addOrEdit-popup {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        z-index: 1000;
        display: none;
        &-bg {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, .5);
        }
        &-content {
            width: 400px;
            background-color: #fff;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            border-radius: 5px;
            padding: 15px;
            &-heading {
                display: flex;
                justify-content: space-between;
                align-items: center;
                &-title {
                    font-size: 24px;
                    font-weight: 500;
                }
                .el-icon-close {
                    font-size: 24px;
                    padding: 6px;
                    cursor: pointer;
                }
            }
            &-body {
                margin-top: 15px;
                .input-group + .input-group {
                    margin-top: 15px;
                }
                .input-group {
                    display: flex;
                    align-items: center;
                    &-label {
                        width: 100px;
                        font-size: 18px;
                        font-weight: 500;
                    }
                    &-input {
                        width: 250px;
                    }
                    &-select {
                        width: 250px;
                    }
                }
            }
            &-footer {
                margin-top: 15px;
                display: flex;
                flex-direction: row-reverse;
            }
        }
    }
    .addOrEdit-popup.show {
        display: block;
    }
</style>